import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.*;

public class TicketThreadDemo {
    ExecutorService threadPool= Executors.newFixedThreadPool(3);

    public static void main(String[] args) {
        TicketThreadDemo demo=new TicketThreadDemo();
        try {
            System.out.println(demo.getPrices());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public Set<Integer> getPrices() throws  InterruptedException{
        Set<Integer> prices= Collections.synchronizedSet(new HashSet<>());
        CompletableFuture<Void> task1=CompletableFuture.runAsync(new Task(123,prices));
        CompletableFuture<Void> task2=CompletableFuture.runAsync(new Task(456,prices));
        CompletableFuture<Void> task3=CompletableFuture.runAsync(new Task(789,prices));
        CompletableFuture<Void> allTask = CompletableFuture.allOf(task1, task2, task3);
        try {
            allTask.get(3,TimeUnit.SECONDS);
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        return prices;

//        threadPool.submit(new Task(123,prices));
//        threadPool.submit(new Task(456,prices));
//        threadPool.submit(new Task(789,prices));
    }

    private class Task implements Runnable{
        Integer productid;
        Set<Integer> prices;
        public Task(Integer productid,Set<Integer> prices){
            this.productid=productid;
            this.prices=prices;
        }

        @Override
        public void run() {
            int price=productid;
//            try {
//                Thread.sleep(4000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            prices.add(price);
        }
    }
}


